<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button-group>
        <el-button :type="filterForm.remove == 1?'success':'default'" @click="filterForm.remove=1;">全部</el-button>
        <el-button :type="filterForm.remove == 3?'success':'default'" @click="filterForm.remove=3">我的账户</el-button>
        <el-button :type="filterForm.remove == 2?'success':'default'" @click="filterForm.remove=2;">回收站</el-button>
      </el-button-group>
    </div>
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="filterForm.keyword" clearable placeholder="账户ID/账户标题/内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="开户银行" prop="bankID">
            <el-select v-model="filterForm.bankID" clearable placeholder="请选择">
              <el-option v-for="item in bankOptions" :key="item.ID" :label="item.title" :value="item.ID" />
            </el-select>
        </el-form-item>
        <el-form-item label="责任人" prop="employee">
          <el-input v-model="filterForm.employee" clearable placeholder="责任人ID/责任人电话/责任人别名" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="货币" prop="currency">
            <el-select v-model="filterForm.status" clearable placeholder="请选择">
              <el-option label="0" value="0">全部</el-option>
              <el-option label="6" value="6">正常</el-option>
              <el-option label="6" value="5">禁用</el-option>
              <el-option label="1" value="1">草稿</el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-select v-model="filterForm.status" clearable placeholder="请选择">
              <el-option label="0" value="0">全部</el-option>
              <el-option label="6" value="6">正常</el-option>
              <el-option label="6" value="5">禁用</el-option>
              <el-option label="1" value="1">草稿</el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
              新建
            </el-button>
            <el-button class="filter-item" style="margin-left: 10px;" type="danger" icon="el-icon-delete" @click="handleRemove">
              删除
            </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column label="账户编码" min-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleDetail(row)">{{ row.accountCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="摘要" min-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleDetail(row)">{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="隶属机构" min-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleDetail(row)">{{ row.company.fullname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="责任人" min-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleDetail(row)">{{ row.employee.fullname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.balance }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status">
            {{ row.status.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="row.status.value == 6" size="mini" @click="handleModifyStatus(row,5)">
            禁用
          </el-button>
          <el-button v-if="row.status.value == 5 " size="mini" @click="handleModifyStatus(row,6)">
            启用
          </el-button>
          <el-button v-if="row.status.value == 1 || row.status.value == 5" size="mini" type="danger" @click="handleDelete(row,$index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.start" :limit.sync="filterForm.perpage" @pagination="getList" />
  </div>
</template>

<script>
import { getAccountList, changeStatusByAccountID, removeAccountByID } from '@/api/finance/account'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      total:0,
      list:[],
      listLoading:false,
      bankOptions:[],
      filterForm: {
        start: 1,
        perpage: 20,
        keyword: undefined,
        sort: '+id'
      },
      catalogueForm: {

      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true
      getAccountList(this.filterForm).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleDetail:function(item){
      this.$router.push({ path: 'account-detail', query: { 'accountID': item.ID }})
    },
    handleCreate:function(){
      this.$router.push({ path: 'account-add'})
    },
    handleUpdate:function(item){
      this.$router.push({ path: 'account-add', query: { 'accountID': item.ID }})
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      changeStatusByAccountID({
        "accountID":row.ID,
        "status":status
      }).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
    this.getList();
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleRemove:function(){

    },
    handleDelete(row, index) {
      removeAccountByID({
        "accountID":row.ID
      }).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
    this.getList();
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
